package com.example.constant;

import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

public enum UserExceptionEnum {

    USER_LOCKED(10001, "user.locked");

    @Getter
    private int code;

    private String message;

    private static MessageSource messageSource;
    public static void setMessageSource(MessageSource messageSource) {
        UserExceptionEnum.messageSource = messageSource;
    }

    UserExceptionEnum(int code, String message) {
        this.code = code;
        this.message = message;
    }

    //通过静态内部类的方式注入bean，并赋值到枚举中
    @Component
    public static class ReportTypeServiceInjector {
        @Autowired
        private MessageSource messageSource;

        @PostConstruct
        public void postConstruct() {
            UserExceptionEnum.setMessageSource(messageSource);
        }
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public String getMessage() {
        return messageSource.getMessage(message, null, LocaleContextHolder.getLocale());
    }
}
